package com.TicTacToe;

import android.app.Activity;
import android.app.Notification;
import android.content.Context;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TicTacToeAiActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
	
	private TicTacToeGame Game;
	private Button btn[];
	private Button restart;
	private TextView Human_num,Computer_num,Draw_num,HumanTXT,ComputerTXT,DrawTXT,Turns;
	private boolean gOver = false;
		
	private int Human_counter = 0 , Computer_counter = 0 , Draw_counter = 0 ;
	private boolean HumanFirst = true ;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // find views
        
        Human_num = (TextView) findViewById(R.id.Human_num);
        Computer_num = (TextView) findViewById(R.id.Computer_num);
        Draw_num = (TextView) findViewById(R.id.Draw_num);
        HumanTXT = (TextView) findViewById(R.id.HumanTXT);
        ComputerTXT= (TextView) findViewById(R.id.ComputerTXT);
        DrawTXT = (TextView) findViewById(R.id.DrawTXT);
        Turns = (TextView) findViewById(R.id.Turns);
        
        
        btn = new Button[Game.getBoard_Size()];
        
        findViewById(R.id.AbsoluteLayout1);
        btn[0] = (Button) findViewById(R.id.btn1);
        btn[1] = (Button) findViewById(R.id.btn2);
        btn[2] = (Button) findViewById(R.id.btn3);
        btn[3] = (Button) findViewById(R.id.btn4);
        btn[4] = (Button) findViewById(R.id.btn5);
        btn[5] = (Button) findViewById(R.id.btn6);
        btn[6] = (Button) findViewById(R.id.btn7);
        btn[7] = (Button) findViewById(R.id.btn8);
        btn[8] = (Button) findViewById(R.id.btnlast);
       restart = (Button) findViewById(R.id.restart);
        
        
        Human_num.setText(Integer.toString(Human_counter));
        Computer_num.setText(Integer.toString(Computer_counter));
        Draw_num.setText(Integer.toString(Draw_counter));
        //create the game
        Game = new TicTacToeGame(); 
        
        startGame();
    }
    
    
		private void startGame(){
        	//clear the board
        	Game.clearBoard();
        	for(int i = 0;i < btn.length;i++){
        		btn[i].setText("");
        		btn[i].setEnabled(true);
        		btn[i].setOnClickListener(this);
        		
        	}
        	//setting who goes first
        	if(HumanFirst){
        		// human
        		Turns.setText("You Go First");
        		HumanFirst = false;
        	}else{
        		// AI
        		Turns.setText("AI is First");
        		int move = Game.getComputerMove();
        		setMove(Game.Computer,move);
        		HumanFirst = true;
        	}
        	
        }
		
		public void setMove(char player, int location) {
			//setting the values of the buttons
			Game.setMove(player, location);
			btn[location].setEnabled(false);
			btn[location].setText(String.valueOf(player));
			//colors of the marks
			if(player == Game.Human){
				btn[location].setTextColor(Color.CYAN);
			}else{
				btn[location].setTextColor(Color.RED);
			}
		}	
		
		    	//what happens when the buttons are clicked
		
		
			public void onClick(View v) {
				
				// setting a new action for the restart button 
				
					restart.setOnClickListener(new View.OnClickListener() {
						
				// 	making the button unclickable in if the game didn't end
						
					public void onClick(View v) {
						if(!gOver){
							
							restart.setOnClickListener(null);
							
						}else{
							
							// restarting the game after it ends
							
						gOver = false;
						Game = new TicTacToeGame();
						startGame();
						
						}
					}
				});
				
				//
				
				// declaring the location and values to be assigned for the clicked buttons
				int location=-1;
				int id = v.getId();
				
				// assigning the values for button clicked
				
				switch(id){	
				
				//the location of each button
								
				case R.id.btn1:
					location=0;
					break;
				case R.id.btn2:
					location=1;
					break;
				case R.id.btn3:
					location=2;
					break;
				case R.id.btn4:
					location=3;
					break;
				case R.id.btn5:
					location=4;
					break;
				case R.id.btn6:
					location=5;
					break;
				case R.id.btn7:
					location=6;
					break;
				case R.id.btn8:
					location=7;
					break;
				case R.id.btnlast:
					location=8;
					break;
		
				}
				
				//checking if the game was over
				
				if(!gOver){
					
					//if the value of a button is still empty continue to play and after the move check for a winner
					
	        		if(btn[location].isEnabled()){
	        			setMove(Game.Human, location);
	        			int winner = Game.checkForWinnerAi();
	        			if(winner == 0 ){
	        				Turns.setText("...");
	        				int move = Game.getComputerMove();
	        				setMove(Game.Computer,move);
	        				winner = Game.checkForWinnerAi();
	        			}
	        			
	        			//if there is an empty button continue to play
	        			
	        			if(winner == 0){
	        				Turns.setText("its your turn");
	        				
	        			// if it was a draw
	        				
	        			}else if(winner ==1){
	        				Turns.setText("its a draw");
	        				Draw_counter++;
	        				Draw_num.setText(Integer.toString(Draw_counter));
	        				gOver = true;

	        		
	        				
	        				// if the you win
	        				
	        			}else if(winner ==2){
	        				Turns.setText("you win !");
	        				Human_counter++;
	        				Human_num.setText(Integer.toString(Human_counter));
	        				gOver = true;      		
	        				
	        				// if the computer wins
	        				
	        			}else{
	        				Turns.setText("you lose !");
	        				Computer_counter++;
	        				Computer_num.setText(Integer.toString(Computer_counter));
	        				gOver = true;

	        			}	        				
	        		}			        			
				}				
			}			
		}						